<?php

namespace app\admin\controller;
use think\Controller;
use think\File;
use think\Request;
use app\common\controller\AdminBaseController;
class Upload extends AdminBaseController
{
	//图片上传
    public function upload(){
    	include_once ('././vendor/aliyun-oss-php-sdk/autoload.php');
			foreach ($_FILES as $name => $file) {
				$info = getimagesize($file['tmp_name']);  
        		$ext = image_type_to_extension($info['2']);  
				$ossClient = new \OSS\OssClient('LTAIaqGtJ3JDXJvv', 'Q2kTcyepAHX0VOR44OtszVoyOKE8gl', 'oss-cn-shanghai.aliyuncs.com');
				$name=time()."_".rand(100000, 999999).$ext;
				try{
					$res=$ossClient->uploadFile("long3",$name,$file['tmp_name']);
					$result['code'] = 1;
					$result['msg'] = '上传成功！';
					$result['src']='https://long3.oss-cn-shanghai.aliyuncs.com/'.$name;
				} catch(OssException $e) {
					$result['code'] = 0;
					$result['msg'] = $file -> $e->getMessage();
				}
			}
		return json($result);
    }  
    
    //上传头像
    public function uploadface() {
			$ret = array('strings' => $_POST, 'error' => '0');
			$fs = array();
			foreach ($_FILES as $name => $file) {
			$fn = $file['name'];
				$ft = strrpos($fn, '.', 0);
				$fm = substr($fn, 0, $ft);
				$fe = substr($fn, $ft);
				$fn = time()."_".rand(100000, 999999) . $fe;
				if(!file_exists('public/uploads/face/'.date('Ymd')))//文件夹不存在，先生成文件夹
					{
				mkdir('public/uploads/face/'.date('Ymd'),0777);//生成今天时间的目录
					}
				$fp = './public/uploads/face/'. date('Ymd').'/'.$fn;
				$fm = '/public/uploads/face/'. date('Ymd').'/'.$fn;
				move_uploaded_file($file['tmp_name'], $fp);
		$fs[$name] = array('name' => $fn, 'url' => $fm, 'type' => $file['type'], 'size' => $file['size']);
			}
			$ret['files'] = $fs;
			echo json_encode($ret); //json 数据
			 //返回格式，必需
	
		exit;
	}

}

